/**  
 * 文 件 名: LoginAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

package com.tongzhou.login.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.login.dao.ILoginDAO;
import com.tongzhou.system.rightsMgmt.model.Right;
import com.tongzhou.system.rightsMgmt.model.Role;
import com.tongzhou.system.userMgr.dao.IUserMgrDAO;
import com.tongzhou.system.userMgr.model.User;
import com.tongzhou.util.DES;
import com.tongzhou.util.Util;

public class LoginAction extends ActionSupport{
	private String id;
	private String username;//用户名
	private String password;//密码
	private String code;//验证码
	private Map<String, Object> session;
	private ILoginDAO dao;
	private IUserMgrDAO userDAO;
	private String judgment;
	private String judgment2;
	
	
	public String inlogin() throws DBException{
		session=ActionContext.getContext().getSession();
        try {
			User user=new User();
			user.setUsername(username);
			user.setPassword(DES.encrypt(password));
			List<User> list=dao.queryUser(user);
			User u=new User();
			if(list.size()>0){
				u=list.get(0);
			}else{
				return "outlogin";
			}
			session.put("username",u.getUsername());
			session.put("password", u.getPassword());
			session.put("id", u.getId());
//			
//			List<Right> rightList=userDAO.getUserUrl(u);
			List<Role> roleList=userDAO.getRoleByUserID(u);
//			List<String> rights=new ArrayList<String>();
//			for(Right r:rightList){
//				if(!Util.checkObjIsNull(r.getUrl())){
//					String[] strs=r.getUrl().split(";");
//					for(int i=0;i<strs.length;i++){
//						if(!Util.checkObjIsNull(strs[i])){
//							rights.add(strs[i].trim());
//						}
//					}
//				}
//			}
			List<String> roles=new ArrayList<String>();
			for(Role r:roleList){
				roles.add(r.getId());
			}
//			session.put("rights", rights);
			session.put("roles", roles);
			username="";
			password="";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String checkCode() throws Exception{
		session=ActionContext.getContext().getSession();
		String codeNO=(String) session.get("rand");//登陆页面传过来的验证码
		if(!codeNO.equals(code)){
			judgment="1";
		}else{
			judgment="0";
		}
		return "yes";
	}
	
	public String checkUser() throws Exception{
		User user=new User();
		user.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
		user.setPassword(DES.encrypt(password));
		List<User> list=dao.queryUser(user);
		if(list.size()>0){
			judgment2="0";
		}else{
			judgment2="1";
		}
		return "yes";
	}
	
	public String outlogin(){
		session=ActionContext.getContext().getSession();
		session.remove("username");
		session.remove("password");
		session.remove("id");
		session.clear();
		return "outlogin";
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	public ILoginDAO getDao() {
		return dao;
	}
	public void setDao(ILoginDAO dao) {
		this.dao = dao;
	}
	public String getJudgment() {
		return judgment;
	}
	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}

	public String getJudgment2() {
		return judgment2;
	}

	public void setJudgment2(String judgment2) {
		this.judgment2 = judgment2;
	}

	public IUserMgrDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(IUserMgrDAO userDAO) {
		this.userDAO = userDAO;
	}
}
